
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.api.multishop.vo.ShopDetailVO;
import com.jf.cloud.api.platform.constant.OfflineHandleEventStatus;
import com.jf.cloud.api.platform.constant.OfflineHandleEventType;
import com.jf.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.jf.cloud.api.platform.feign.OfflineHandleEventFeignClient;
import com.jf.cloud.api.platform.vo.OfflineHandleEventVO;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.multishop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 下线处理事件
 *
 * @author zz
 * @date 2021-01-15 17:46:26
 */
@RestController("shopOfflineHandleEventController")
@RequestMapping("/mp/shop_offline_handle_event")
@Tag(name = "admin-下线处理事件")
public class ShopOfflineHandleEventController {

    @Autowired
    private OfflineHandleEventFeignClient offlineHandleEventFeignClient;
    @Autowired
    private ShopDetailService shopDetailService;


    @PostMapping("/offline")
    @Operation(summary = "下线店铺" , description = "下线店铺")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        ShopDetailVO shopDetail = shopDetailService.getByShopId(offlineHandleEventDto.getHandleId());
        if (shopDetail == null) {
            throw new LuckException("该店铺不存在或者已删除");
        }

        // 添加下线处理记录
        offlineHandleEventDto.setHandleType(OfflineHandleEventType.PROD.getValue());
        offlineHandleEventDto.setHandleId(shopDetail.getShopId());
        inspectionRequest(offlineHandleEventFeignClient.save(offlineHandleEventDto));
        // 更新店铺状态为下线
        shopDetailService.changeSpuStatus(shopDetail.getShopId(), StatusEnum.OFFLINE.value());

        // 移除缓存
        shopDetailService.removeCacheByShopId(shopDetail.getShopId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_offline_handle_event_by_prodId/{prodId}")
    @Operation(summary = "获取最新下线店铺的事件" , description = "通过prodId获取最新下线店铺的事件")
    public ServerResponseEntity<OfflineHandleEventVO> getOfflineHandleEventByProdId(@ParameterObject @PathVariable Long prodId) {
        ServerResponseEntity<OfflineHandleEventVO> offlineHandleEventResponse =
                offlineHandleEventFeignClient.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.SHOP.getValue(), prodId, null);
        return ServerResponseEntity.success(offlineHandleEventResponse.getData());
    }

    @PostMapping("/shop_audit")
    @Operation(summary = "审核店铺" , description = "审核店铺")
    public ServerResponseEntity<Void> prodAudit(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        Long shopId = offlineHandleEventDto.getHandleId();
        inspectionRequest(offlineHandleEventFeignClient.auditOfflineEvent(offlineHandleEventDto));
        // 审核通过,更新店铺为营业状态
        if (Objects.equals(offlineHandleEventDto.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            shopDetailService.changeSpuStatus(shopId, StatusEnum.ENABLE.value());
        } else if (Objects.equals(offlineHandleEventDto.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            shopDetailService.changeSpuStatus(shopId, StatusEnum.OFFLINE.value());
        }

        // 移除缓存
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/audit_apply")
    @Operation(summary = "违规店铺提交审核" , description = "违规店铺提交审核")
    public ServerResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        Long shopId = offlineHandleEventDto.getHandleId();
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(offlineHandleEventDto.getHandleId());
        if (shopDetailVO == null) {
            // 店铺信息不存在
            throw new LuckException("店铺信息不存在");
        }
        inspectionRequest(offlineHandleEventFeignClient.updateToApply(offlineHandleEventDto));
        // 更新店铺为待审核状态
        shopDetailService.changeSpuStatus(shopId, StatusEnum.WAIT_AUDIT.value());
        return ServerResponseEntity.success();
    }
    private void inspectionRequest(ServerResponseEntity serverResponseEntity) {

    }
}
